package lupita.controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lupita.model.vos.FileSystemItem;
import lupita.persistence.MySQLImpl;

/**
 * Servlet implementation class DetailsFile
 */
public class DetailsFile extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MySQLImpl mysql;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DetailsFile() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		try {
			mysql = new MySQLImpl();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		ResultSet result;
		
		HttpSession session = request.getSession();
		List<FileSystemItem> listFileRepo = (List<FileSystemItem>) session.getAttribute("fileRepoList");
		
		String index = request.getParameter("idFile");
		int userID = (Integer)session.getAttribute("IdUser");
		int fileIndex = -1;
		
		if (index != null){
			fileIndex = Integer.parseInt(index);
		}
		
		
		String pageToForward = "/resultDetails.jsp";
		
		System.out.println("lista: " + listFileRepo);
		System.out.println("fileIndex: " + fileIndex);
		
		
		FileSystemItem fileRepo = listFileRepo.get(fileIndex);
		System.out.println("item: " + fileRepo.getFileName());
		String fileName =  fileRepo.getFileName();
		//List<TagFile> tagList = tagDAO.getTopTagFile(fileRepo.getFileName(), 4);
		
		
		//FileRepo findFileRepo = fileRepoDAO.findByName(fileRepo.getFileName());
		//if(findFileRepo!=null) {
			//fileRepo = findFileRepo;
		//}
		
		//request.setAttribute("tagList", tagList);
		request.setAttribute("fileRepo", fileRepo);
		request.setAttribute("fileindex", index);
		
		String query = "SELECT * FROM filerating fr JOIN file f on fr.idFile = f.id WHERE f.name = '" + fileName + "' AND fr.idUser = " + userID;
		System.out.println("queryrating" + query);
		
		String res = "1";
		try {
			mysql.openConnection();
			System.out.println("paso:");
			result=  mysql.selectSQL(query);
			System.out.println("pasosssss:");
			if(result.next()){//pregunto si hay un registro
				res = "1";
				request.setAttribute("isRating", res);
				System.out.println("paso1");
			}else{
				res = "0";
				request.setAttribute("isRating", res);
				System.out.println("paso2");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("paso3");
		
		
		RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(pageToForward);			
		requestDispatcher.forward(request, response);
		
/*
		  PrintWriter out = response.getWriter();
		  out.println("You have successfully made Ajax Call:");*/
		/*RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher("/resultDetails.jsp");
		requestDispatcher.forward(request, response);*/
	}

}
